package com.exrobot.module.rbt.controller.admin.robotoperationlog;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.CommonResult;
import com.exrobot.framework.common.util.object.BeanUtils;
import static com.exrobot.framework.common.pojo.CommonResult.success;

import com.exrobot.framework.excel.core.util.ExcelUtils;

import com.exrobot.framework.apilog.core.annotation.ApiAccessLog;
import static com.exrobot.framework.apilog.core.enums.OperateTypeEnum.*;

import com.exrobot.module.rbt.controller.admin.robotoperationlog.vo.*;
import com.exrobot.module.rbt.dal.dataobject.robotoperationlog.RobotOperationLogDO;
import com.exrobot.module.rbt.service.robotoperationlog.RobotOperationLogService;

@Tag(name = "管理后台 - 车辆运行日志")
@RestController
@RequestMapping("/rbt/robot-operation-log")
@Validated
public class RobotOperationLogController {

    @Resource
    private RobotOperationLogService robotOperationLogService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆运行日志")
    @PreAuthorize("@ss.hasPermission('rbt:robot-operation-log:create')")
    public CommonResult<Long> createRobotOperationLog(@Valid @RequestBody RobotOperationLogSaveReqVO createReqVO) {
        return success(robotOperationLogService.createRobotOperationLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆运行日志")
    @PreAuthorize("@ss.hasPermission('rbt:robot-operation-log:update')")
    public CommonResult<Boolean> updateRobotOperationLog(@Valid @RequestBody RobotOperationLogSaveReqVO updateReqVO) {
        robotOperationLogService.updateRobotOperationLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆运行日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rbt:robot-operation-log:delete')")
    public CommonResult<Boolean> deleteRobotOperationLog(@RequestParam("id") Long id) {
        robotOperationLogService.deleteRobotOperationLog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除车辆运行日志")
                @PreAuthorize("@ss.hasPermission('rbt:robot-operation-log:delete')")
    public CommonResult<Boolean> deleteRobotOperationLogList(@RequestParam("ids") List<Long> ids) {
        robotOperationLogService.deleteRobotOperationLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆运行日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rbt:robot-operation-log:query')")
    public CommonResult<RobotOperationLogRespVO> getRobotOperationLog(@RequestParam("id") Long id) {
        RobotOperationLogDO robotOperationLog = robotOperationLogService.getRobotOperationLog(id);
        return success(BeanUtils.toBean(robotOperationLog, RobotOperationLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆运行日志分页")
    @PreAuthorize("@ss.hasPermission('rbt:robot-operation-log:query')")
    public CommonResult<PageResult<RobotOperationLogRespVO>> getRobotOperationLogPage(@Valid RobotOperationLogPageReqVO pageReqVO) {
        PageResult<RobotOperationLogDO> pageResult = robotOperationLogService.getRobotOperationLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotOperationLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆运行日志 Excel")
    @PreAuthorize("@ss.hasPermission('rbt:robot-operation-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRobotOperationLogExcel(@Valid RobotOperationLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotOperationLogDO> list = robotOperationLogService.getRobotOperationLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆运行日志.xls", "数据", RobotOperationLogRespVO.class,
                        BeanUtils.toBean(list, RobotOperationLogRespVO.class));
    }

}